import 'package:app/common/colors.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/pages/contract_module/contract_controller.dart';
import 'package:app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../common/app_theme.dart';
import '../../common/tool.dart';
import '../../utils/search_module.dart';
import 'detail_module/contract_detail_view.dart';
import 'contract_model.dart';

class ContractView extends GetView{

  ContractController controller = Get.put(ContractController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar:BaseAppBar(preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
      child: Text('合同'),action: [
        TextButton(onPressed: ()async{
         var data = await Get.toNamed(AppRoutes.createContract,arguments: {'contract_id':0,});
         if(data != null){
           controller.requestData();
         }
          }, child: Text('新建',style: TextStyle(color: Colors.white),)),
        ],),
      body: GetBuilder<ContractController>(builder: (_){
        return GestureDetector(
          onTap: (){
            SearchModule.closeKeyboard(context);
          },
          onPanDown: (_) {
            /// 列表滑动响应
            SearchModule.closeKeyboard(context);
          },

          child: Column(
            children: [
              Container(
                color: Colors.white,
                height: 50,
                child: Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 15),
                      child: Row(
                        children: [
                          GestureDetector(
                            child: Text(
                              controller.replyType,
                              style: appThemeData.textTheme.bodyText1!
                                  .copyWith(fontSize: 13),
                            ),
                            onTap: () => controller.topClick(),
                          ),
                          controller.isTopShow == false
                              ? Image.asset(
                            'images/arrow-down.png',
                            width: 12,
                            height: 12,
                          )
                              : Image.asset(
                            'images/arrow-up.png',
                            width: 12,
                            height: 12,
                            color: AppColor.themeColor,
                          )
                        ],
                      ),
                    ),
                    Container(
                        decoration: BoxDecoration(
                            color: AppColor.bgColor,
                            borderRadius:
                            BorderRadius.all(Radius.circular(13))),
                        width: Get.width - 150,
                        alignment: Alignment.centerLeft,
                        margin: EdgeInsets.only(top: 0, left: 10, right: 10),
                        padding: EdgeInsets.only(left: 15),
                        height: 30,
                        child: Row(
                          children: [
                            Icon(
                              Icons.search,
                              size: 20,
                              color: AppColor.smallTextColor,
                            ),
                            Container(
                              alignment: Alignment.center,
                              color: Colors.transparent,
                              width: Get.width - 200,
                              height: 30,
                              child: TextField(
                                controller: controller.editingController,
                                onChanged: (values) {
                                  // requestData(search: values);
                                  controller.requestData(search: values);
                                },
                                decoration: InputDecoration(
                                    isCollapsed: true,
                                    border: InputBorder.none,
                                    hintText: '请输入关键词',
                                    hintStyle: TextStyle(
                                        color: AppColor.hintColor,
                                        fontSize: 13)),
                                style: TextStyle(
                                    color: AppColor.smallTextColor,
                                    fontSize: 14),
                              ),
                            )
                          ],
                        )),
                  ],
                ),
              ),
             Expanded(child:  EasyRefresh.custom(
                 controller: controller.easyRefreshController,
                 // header: MaterialHeader(),
                 footer: MaterialFooter(),
                 onLoad: ()=>controller.onLoad(),
                 onRefresh: ()=>controller.pullToRefresh(),
                 slivers: [
                   SliverList(
                     delegate: _mySliverChildBuilderDelegate(),
                   ),
                 ]),),
            ],
          )
        );
      }),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            ContractItemModel model = controller.dataArr![index];
        return Container(
          // padding: EdgeInsets.all(10),
          margin: EdgeInsets.all(5),
          child:GestureDetector(
            onTap: (){
              // Get.to(ContractDetailView());
              Get.toNamed(AppRoutes.contractDetail,arguments: model.id);
            },
            child:  Card(
              elevation: 0,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    padding: EdgeInsets.all(5),
                    width: Get.width-100,
                    child: Text('合同名称：${model.name}',style: appThemeData.textTheme.bodyText1!.copyWith(
                      fontSize: 16,fontWeight: FontWeight.w700
                    ),),
                  ),
                  Container(
                    padding: EdgeInsets.all(5),
                    width: Get.width-100,
                    child: Text('员工名称：${model.employeeName}',style: appThemeData.textTheme.subtitle1,),
                  ),
                  Container(
                    padding: EdgeInsets.all(5),

                    width: Get.width-100,
                    child: Text('开始时间：${model.dateStart}',style: appThemeData.textTheme.subtitle1,),
                  ),
                GestureDetector(
                  onTap: ()async{
                    var data = await Get.toNamed(AppRoutes.createContract,arguments: {'contract_id':model.id,'id':model.id});
                    if(data != null){
                      controller.requestData();
                    }
                  },

                  child:Container(
                    padding: EdgeInsets.all(5),
                    // width: Get.width-100,
                    child: Row(
                      children: [
                        Image.asset('images/edit_icon.png',width: 15,height: 15,),
                        Text('修改',style: appThemeData.textTheme.subtitle1,)
                      ],
                    ),
                  ),
                ),
                ],
              ),
            ),
          ),
        );
      },
      childCount: controller.dataArr!.length,
    );
  }


}